home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / warp / Mdepend.awk < prev    next >
Encoding:
AWK Script  |  1995-05-03  |  5.6 KB  |  183 lines

  1. ##
  2. ### "Mdepend.awk" -  Scan for #includes and generate make dependency lines
  3. ###
  4. ###    v1.1(01)   20 Feb 1990    John Elliott IV
  5. ##
  6.  
  7. BEGIN {
  8.     version="1.1(01)";
  9.     print "READING:" > "/dev/tty";
  10. }
  11.  
  12. lastfile != FILENAME {
  13.     printf("    %s:\n", FILENAME) > "/dev/tty";
  14.     lastfile = FILENAME;
  15.  
  16.     # Keep track of all files scanned
  17.     files[nfiles++] = lastfile;
  18. }
  19.  
  20. /^#[     ]*include[     ]*"/ {
  21. ###################################################
  22. # Find the opening quote or less-than
  23. ###################################################
  24.     for (i = 1; i <= length; ++i) {
  25.         opening = substr($0, i, 1);
  26.         if (opening == "\"" || opening == "<")
  27.             break;
  28.     }
  29. ###################################################
  30. # Find the closing quote or greater-than
  31. ###################################################
  32.     closing = opening;
  33.     if (closing == "<")  closing = ">";
  34.     len = index(substr($0, ++i, length), closing);
  35.     if (len == 0)
  36.         len = length + 1;
  37.     else
  38.         len = len - 1;
  39.  
  40. ###################################################
  41. # Extract the file name
  42. ###################################################
  43.     file = substr($0, i, len);
  44.     # printf("\t%-24s   substr(\"%s\", %d, %d)\n",
  45.     #     opening file closing, $0, i, len) > "/dev/tty";
  46.  
  47. ###################################################
  48. # Remember if this is a system include file
  49. ###################################################
  50.     if (closing == ">")
  51.         file = "<" file ">";
  52.  
  53. ###################################################
  54. # Show what we found
  55. ###################################################
  56.     #printf("\t%s\n", file) > "/dev/tty";
  57.  
  58.  
  59. #######################################################################
  60. # Remember what we've found:
  61. #    sources[] -  A list of files that #include and who they include
  62. #                 (indexed by filename and numerically indexed
  63. #                  so we can loop thru all names)
  64. #######################################################################
  65.     if (sources[FILENAME] == "")
  66.         sources[nsources++] = FILENAME;
  67.     sources[FILENAME] = sources[FILENAME] " " file;
  68. }
  69.  
  70.  
  71. END {
  72.     printf("\nPROCESSING:\n") > "/dev/tty";
  73.  
  74. #######################################################################
  75. # Now, for each file, we look at each file it includes.
  76. # If it is listed in the sources, look to see if THAT file
  77. # includes other files, and if it does, check THAT file, & etc
  78. # until we have a list of all files this target depends on.
  79. #######################################################################
  80.     for (i = 0; i < nsources; ++i) {
  81.         sname = sources[i];
  82.         printf("\t%s", sname) > "/dev/tty";
  83.     ###############################################
  84.     # Look at all files "sname" includes
  85.     ###############################################
  86.         oj = nj = split(sources[sname], list);
  87.         for (j = 1; j <= nj; ++j)
  88.             list[list[j]] = j;   # Remember which are here already
  89.         for (j = 1; j <= nj; ++j) {
  90.             iname = list[j];
  91.         #######################################################
  92.         # If this file itself includes files, add them to the
  93.         # to-be-scanned list if they're not already there
  94.         # (Yes, I know; this could go on forever)
  95.         #######################################################
  96.             if (sources[iname] != "") {
  97.             nk = split(sources[iname], new);
  98.             ##################################################
  99.             # For each file that this one includes, see if
  100.             # we have already added it to our list.  If not,
  101.             # add it to the "depends-on" list.
  102.             ##################################################
  103.             for (k = 1; k <= nk; ++k) {
  104.                 if (list[new[k]] == "") {
  105.                 #if (oj == nj)
  106.                 #    printf(" -  adding:") > "/dev/tty";
  107.                 #printf(" %s", new[k]) > "/dev/tty";
  108.                 list[++nj] = new[k];
  109.                 list[new[k]] = nj;
  110.                 sources[sname] = sources[sname] " " new[k];
  111.                 }
  112.             }
  113.             }
  114.         }
  115.         printf("\n") > "/dev/tty";
  116.     }
  117.  
  118.     printf("Updating make dependency lines\n") > "/dev/tty";
  119.     ##################################################################
  120.     #  Tell `ed' to change everything between our two special lines:
  121.     ##################################################################
  122.     printf("/^# DO NOT DELETE THIS LINE/,/^# ALSO DO NOT DELETE THIS/ c\n");
  123.     printf("# DO NOT DELETE THIS LINE - \"make depend\" requires it.\n");
  124.     printf("# Generated by Mdepend.awk version %s\n", version);
  125.     printf("# The dependencies were generated by examining the following files:");
  126.     ll = 80;
  127.     for (i = 0; i < nfiles; ++i) {
  128.         file = files[i];
  129.         wl = length(file);
  130.         if (ll + wl + 1 > 78) {
  131.             printf("\n#    ");
  132.             ll = 5;
  133.         }
  134.         printf(" %s", file);
  135.         ll += wl + 1;
  136.     }
  137.     printf("\n");
  138.  
  139.     for (i = 0; i < nsources; ++i) {
  140.         file = sources[i];
  141.         # Find the extension (if any)
  142.         for (extndx = length(file); extndx > 0; --extndx) {
  143.             if (substr(file, extndx, 1) == ".")
  144.                 break;
  145.         }
  146.         if (extndx == 0)
  147.             ext = "";
  148.         else
  149.             ext = substr(file, extndx, length(file));
  150.         # In case they forget and give us a non-code-generating file...
  151.         if (ext == ".h")
  152.             continue;
  153.  
  154.         # Now chop any path prefix
  155.         for (ndent = length(file); ndent > 0; --ndent) {
  156.             if (substr(file, ndent, 1) == "/")
  157.                 break;
  158.         }
  159.         ++ndent;    # Skip the slash, or start at '1'
  160.  
  161.         # Finally, output the dependency lines
  162.         printf("%s.o:", substr(file, ndent, extndx - ndent));
  163.         deps = sources[file];
  164.         nj = split(deps, list);
  165.         ll = length(file) + 1;
  166.         for (j = 1; j <= nj; ++j) {
  167.             wl = length(list[j]);
  168.             if (ll + wl + 1 > 78) {
  169.                 printf("\\\n\t%s", list[j]);
  170.                 ll = 8 + wl;
  171.             }
  172.             else {
  173.                 ll += wl + 1;
  174.                 printf(" %s", list[j]);
  175.             }
  176.         }
  177.         printf("\n");
  178.     }
  179.     printf("# ALSO DO NOT DELETE THIS LINE - \"make depend\" requires it.\n");
  180.     # Finally, tell ed to quit changing, write the new file out, and quit
  181.     printf(".\nw\nq\n");
  182. }
  183.